//
//  NavigationRouteViewController.m
//  newTestGit
//
//  Created by fangjs on 16/3/11.
//  Copyright © 2016年 Hongshi. All rights reserved.
//

#import "NavigationRouteViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface NavigationRouteViewController () <MKMapViewDelegate>
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
@property (nonatomic , strong) CLGeocoder *geoC;
@end

@implementation NavigationRouteViewController

- (CLGeocoder *)geoC {
    if (_geoC == nil) {
        _geoC = [[CLGeocoder alloc] init];
    }
    return _geoC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor magentaColor];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.geoC geocodeAddressString:@"杭州" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
       
        CLPlacemark *hzP = [placemarks firstObject];
        [self.geoC geocodeAddressString:@"上海" completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
            CLPlacemark *shP = [placemarks firstObject];
            [self getRouteWithBeginPL:hzP andEndPL:shP];
        }];
    }];
}

- (void) getRouteWithBeginPL:(CLPlacemark *) beginP andEndPL:(CLPlacemark *) endPL {
    
    MKCircle *circle = [MKCircle circleWithCenterCoordinate:beginP.location.coordinate radius:10000];
    [self.mapView addOverlay:circle];
    
    MKCircle *circle1 = [MKCircle circleWithCenterCoordinate:endPL.location.coordinate radius:10000];
    [self.mapView addOverlay:circle1];
    
    MKDirectionsRequest *request = [[MKDirectionsRequest alloc] init];
    
    //出发地
    CLPlacemark *cPLBegin = beginP;
    MKPlacemark *mPLBegin = [[MKPlacemark alloc] initWithPlacemark:cPLBegin];
    MKMapItem *beginPlace = [[MKMapItem alloc] initWithPlacemark:mPLBegin];
    request.source = beginPlace;
    
    //目的地
    CLPlacemark *cPLEnd = endPL;
    MKPlacemark *mPLEnd = [[MKPlacemark alloc] initWithPlacemark:cPLEnd];
    MKMapItem *endPlace = [[MKMapItem alloc] initWithPlacemark:mPLEnd];
    request.destination = endPlace;
    
    MKDirections *directions = [[MKDirections alloc] initWithRequest:request];
    [directions calculateDirectionsWithCompletionHandler:^(MKDirectionsResponse * _Nullable response, NSError * _Nullable error) {
        /**
         *  MKDirectionsResponse
         routes : 路线数组MKRoute
         
         */
        /**
         *  MKRoute
         name : 路线名称
         distance : 距离
         expectedTravelTime : 预期时间
         polyline : 折线(数据模型)
         steps
         */
        /**
         *  steps <MKRouteStep *>
         instructions : 行走提示
         */

        [response.routes enumerateObjectsUsingBlock:^(MKRoute * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSLog(@"%@----%f",obj.name,obj.distance);
            
            MKPolyline *polyline = obj.polyline;
            [self.mapView addOverlay:polyline];
    }];
        
    }];
}

#pragma mark -- MKMapViewDelegate
-(MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay {
    
    if ([overlay isKindOfClass:[MKCircle class]]) {
        MKCircleRenderer *circleReder = [[MKCircleRenderer alloc] initWithOverlay:overlay];
        circleReder.fillColor = [UIColor orangeColor];
        circleReder.alpha = 0.5;
        return circleReder;
    }
    
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineRenderer *render = [[MKPolylineRenderer alloc] initWithOverlay:overlay];
        render.lineWidth = 5.0;
        render.strokeColor = [UIColor colorWithRed:1.000 green:0.435 blue:0.812 alpha:1.000];
        
        return render;
    }
    return nil;
}

@end





























